﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChainAnalises.Classes.DivizionToAccords;
using ChainAnalises.Classes.DivizionToAccords.AuxiliaryClasses;

namespace Segmentator.Dividers
{
    class Cross
    {
        public Cross()
        {
        }

        public IDataForStd MakeFilteration(IDataForStd std, int length, int i)
        {
            if (std.Positions.Count == 0)
            {
                return new NullDataForStd();
            }
            int pos = 1;
            int pred = (int)std.Positions[0];
            while (std.Positions.Count > pos)
            {
                if (((int)std.Positions[pos] - pred) < i)
                {
                    std.Positions.RemoveAt(pos);
                    std.DecrimentN();
                }
                else
                {
                    pred = (int)std.Positions[pos];
                }
                pos++;
            }
            return std;
        }

       
        public IDataForStd MakeCross(IDataForStd std, int length)
        {
            int pos = 0;
           
            while (std.Positions.Count > pos)
            {
                for (int i = 0; i < StdCalculator._collector.Size(); i++)
                {
                    if (std.Positions.Count == 0)
                    {
                        return new NullDataForStd();
                    }
                    int in_left = (int)std.Positions[pos];
                    int in_right = in_left + length;
                    for (int j = 0; j < StdCalculator._collector.GetStd(i).Positions.Count; j++)
                    {
                        if (j > pos ) break;
                        int left = StdCalculator._collector.GetStd(i).n;
                        int right = left + StdCalculator._collector.GetLength(i);
                        bool cross = ((in_left < left) && (in_right < right)) ||
                                     ((in_left > left) && (in_right > right));
                        if (!cross)
                        {
                            if (std.Positions.Count == 0)
                            {
                                return new NullDataForStd();
                            }
                            std.Positions.RemoveAt(pos);
                            std.DecrimentN();
                        }
                    }
                }
                pos= pos + 1;
            }
            return std;
        }
    }
}
